package cn.sunxyz.spider.queue.support;


import cn.sunxyz.spider.queue.Queue;
import cn.sunxyz.spider.queue.UrlQueueManager;

import java.util.Set;

/**
 * Created by yangrd on 2018/5/30
 **/
public abstract class AbstractScheduler implements UrlQueueManager {

    private Queue<String> queue;

    private Set<String> visit;

    public AbstractScheduler(Queue<String> queue, Set<String> visit) {
        this.queue = queue;
        this.visit = visit;
    }

    @Override
    public void push(String request) {
        if (!isVisit(request)) {
            queue.push(request);
            visit.add(request);
        }
    }

    @Override
    public String pop() {
        return queue.pop();
    }

    @Override
    public int size() {
        return queue.size();
    }

    boolean isVisit(String request) {
        return visit.contains(request);
    }
}
